<?php

class Model_login extends Model {

    function Model_login() {
        parent::Model();
    }


    /**
     * Verificare user la login
     * @return object FALSE / user record altfel
     * @param object $sUserName
     * @param object $sPsswd
     */
    function checkUserPassword($sUserName, $sPsswd) {
        $this->db->where("username", $sUserName ) ;
        $this->db->where("password", md5($sPsswd) ) ;
        $this->db->where("available", 1) ;
        $oQuery = $this->db->get("user");
        if ($oQuery->num_rows() > 0) {
            return $oQuery->row_array();
        }
        return FALSE;
    }

    function changePassword($iUserId, $sPassword) {
        $data = array('password' => md5($sPassword));
        $this->db->where('user_id', $iUserId);
        return $this->db->update('user', $data);
    }


    function backup() {
        //scoatere date de backup din fisierul config.php
        $sBackupDir = $this->config->item("backup_dir");
        $sBackupDb = $this->config->item("backup_db");
        $iBackupNr = $this->config->item("backup_nr");

        //selectez inregistrarea asociata zilei curente (daca exista)
        $query = $this->db->query("select * from backup where date(`date`)=date(now())");
        if ($query->num_rows() == 0) {

            $this->db->limit("1", "0");
            $this->db->order_by("backup_id", "desc");
            $query = $this->db->get("backup");

            //scot numarul asociat ultimului backup
            $iLastNr = $query->row()->nr;

            //daca nu exista nici o inregistrare, $iLastNr devine 1
            if ($iLastNr == '') {
                $iLastNr = 1;
            }
            else {
                $iLastNr++;
            }

            //daca se depaseste limita backup-uri se revine la iteratia 1
            if ($iLastNr == $iBackupNr) $iLastNr = 1;
            //inserez in baza de date ziua si numarul backup-ului
            $this->db->query("insert into backup (date,nr) values (now(),'" . $iLastNr . "')");
            $command = "F:/wamp/bin/mysql/mysql5.5.8/bin/mysqldump --opt -h localhost -u root " . $sBackupDb . " --default-character-set=utf8 --add-drop-table --complete-insert  > " . $sBackupDir . "backup_" . $iLastNr . ".sql";
            system($command);
        }
    }
}

/* End of file model_login.php */
/* Location: ./system/application/models/model_login.php */